package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogChooseBinding;

/**
 * 相册、拍照
 */
public abstract class ChoosePhotoDialog {
    private Dialog dialog;
    private final String title1;
    private final String title2;

    public ChoosePhotoDialog(Activity activity, String title1, String title2) {
        this.title1 = title1;
        this.title2 = title2;
        getDialog(activity);
    }

    private void getDialog(Activity activity) {
        DialogChooseBinding binding = DialogChooseBinding.inflate(LayoutInflater.from(activity));
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        // lp.x = 266; // 新位置X坐标
        // lp.y = Utils.dip2px(activity, 300); // 新位置Y坐标
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);
        binding.tvChooseOne.setText(title1);
        binding.tvChooseTwo.setText(title2);
        binding.tvCancel.setOnClickListener(v -> dismissDialog());
        binding.tvChooseOne.setOnClickListener(view1 -> {
            chooseOne();
            dismissDialog();
        });

        binding.tvChooseTwo.setOnClickListener(view2 -> {
            chooseTwo();
            dismissDialog();
        });
    }

    public abstract void chooseOne();

    public abstract void chooseTwo();

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
